<template>
  <el-container class="Big_box">
    <!-- 头部 -->
    <el-header>
      <div class="logo">
        <img src="../assets/下载.jpg" /><span>麦兜电商后台管理系统</span>
      </div>
      <el-button @click="back">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="flag ? '65px' : '200px'">
        <div class="toggle_btn" @click="toggle">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="flag"
          :collapse-transition="false"
          :router="true"
          :default-active="Rurl"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in lists"
            :key="item.id"
          >
            <!-- 一级模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconLists[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + Sitem.path"
              v-for="Sitem in item.children"
              :key="Sitem.id"
              @click="saveactive('/' + Sitem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <span>{{ Sitem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容区域 -->
      <el-main>
        <!-- 路由站位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 保存点击的地址
      Rurl: '',
      flag: false,
      lists: [],
      iconLists: {
        125: 'el-icon-s-custom',
        103: 'el-icon-s-shop',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-marketing',
      },
    }
  },
  created() {
    this.getList()
    //拿到上次点击的地址
    this.Rurl = window.sessionStorage.getItem('activepath')
  },
  methods: {
    back() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有菜单
    async getList() {
      const { data: res } = await this.$http.get('menus')
      //   console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.lists = res.data
    },
    toggle() {
      this.flag = !this.flag
    },
    // 保存链接的激活状态
    saveactive(activepath) {
      // 把拿到的地址保存到sessionStorage里面
      window.sessionStorage.setItem('activepath', activepath)
      // 重新给Rurl赋值
      this.Rurl = activepath
    },
  },
}
</script>

<style lang="less" scoped>
.Big_box {
  height: 100%;
}
header {
  background-color: #373d41;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .logo {
    display: flex;
    align-items: center;
    img {
      width: 61px;
      height: 56px;
      border-radius: 50%;
    }
    span {
      font-size: 25px;
      color: #fff;
      margin-left: 8px;
    }
  }
}
aside {
  background-color: #333744;
  .el-menu {
    border-right: 0;
  }
}
main {
  background-color: #eaedf1;
}
.toggle_btn {
  background-color: #666;
  text-align: center;
  font-size: 16px;
  line-height: 40px;
  color: #fff;
  letter-spacing: 0.3em;
  cursor: pointer;
}
</style>